﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BitLevel
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 5, y = 3, z;// 5=00......0101 , 3 = 00......0011

            z = x << 1;   // décale le premier opérande vers la gauche du nombre de bits spécifié par le second 
            Console.WriteLine("5 << 1  = " + z);

            z = ~x;   // ~5=11......1010 (soit -6 en complément à deux)
            Console.WriteLine("~5  = " + z);

            z = x & y; //00......0101 & 00......0011 = 00......001(soit 1 en complément à deux)
            Console.WriteLine("5 & 3 = " + z);

            z = x | y;  //00......0101 | 00......0011 = 00......111(soit 7 en complément à deux)
            Console.WriteLine("5 | 3 = " + z);

            z = x ^ y; //00......0101 ^ 00......0011 = 00......0110(soit 6 en complément à deux)
            Console.WriteLine("5 ^ 3 = " + z);

            int i = 1;
            long lg = 1;
            // Shift i one bit to the left. The result is 2.
            Console.WriteLine("0x{0:x}", i << 1);
            // In binary, 33 is 100001. Because the value of the five low-order
            // bits is 1, the result of the shift is again 2. 
            Console.WriteLine("0x{0:x}", i << 33);
            // Because the type of lg is long, the shift is the value of the six
            // low-order bits. In this example, the shift is 33, and the value of
            // lg is shifted 33 bits to the left.
            //     In binary:     10 0000 0000 0000 0000 0000 0000 0000 0000 
            //     In hexadecimal: 2    0    0    0    0    0    0    0    0
            Console.WriteLine("0x{0:x}", lg << 33);

            /////////

            string s1, s2 = "heur", s3 = "jour";

            s1 = "bon";
            s1 += s2;
            Console.WriteLine(s1);

            s1 = "bon";
            s1 += s3;
            Console.WriteLine(s1);

            x = 5;
            int x0 = x; // 5 =0...0101
            x <<= 2;  // on décale à gauche de 2 bits soit : 0...010100
            Console.WriteLine("avant (x= 5): " + x0 + ", après (x <<=2) : " + x);

            Console.ReadLine();
        }
    }
}
